import 'package:flutter/material.dart';

// 记得添加依赖 animations: ^1.1.1

void main() => runApp(RootApp());

// 定义根目录Wigdget
class RootApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 根视图
    return MaterialApp(
      theme: ThemeData(primaryColor: Colors.grey[200]),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  TabController _tabController;
  List list = [];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabController = new TabController(vsync: this, length: 3);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: NestedScrollView(
        body: buildBodyWidget(),
        headerSliverBuilder: (BuildContext context, bool b) {
          return [
            buildSliverAppbar(),
          ];
        },
      ),
    );
  }

  buildSliverAppbar() {
    return SliverAppBar(
      floating: false,
      title: buildHeader(),
      pinned: true,
      expandedHeight: 400,
      flexibleSpace: FlexibleSpaceBar(
        background: buildBackground(),
      ),
      bottom: TabBar(controller: _tabController, tabs: [
        Tab(text: '标签一'),
        Tab(text: '标签二'),
        Tab(text: '标签三'),
      ]),
    );
  }

  buildHeader() {
    return Container(
      width: double.infinity,
      height: 38,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(
          Radius.circular(20),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(Icons.search_rounded, size: 18),
          SizedBox(width: 8),
          Text("搜索", style: TextStyle(fontSize: 18)),
        ],
      ),
    );
  }

  buildBackground() {
    return Column(children: <Widget>[
      Container(
          height: 240,
          // child: Text('你好呀，等我学完，再移动过来'),
          child: bannerHomePage()),
      Container(
        child: Row(
          children: [
            Expanded(
              child: Image.asset("images/3.0/logo.jpg",
                  height: 140, fit: BoxFit.fill),
            ),
            Expanded(
              child: Image.asset("images/3.0/logo.jpg",
                  height: 140, fit: BoxFit.fill),
            )
          ],
        ),
      ),
    ]);
  }

  // 轮播照片
  List imgList = [
    'images/3.0/logo.jpg',
    'images/3.0/logo.jpg',
    'images/3.0/logo.jpg',
    'images/3.0/logo.jpg',
  ];
  int currentIndex = 500;
  bannerHomePage() {
    return PageView.builder(
      itemBuilder: (BuildContext context, int index) {
        return buildPageViewItemWidget(index);
      },
      itemCount: list.length * 1000,
    );
  }

  buildPageViewItemWidget(int index) {
    return Image.asset(imgList[index], fit: BoxFit.fill);
  }

  buildBodyWidget() {
    return TabBarView(
      controller: _tabController,
      children: [
        itemPage(1),
        itemPage(2),
        itemPage(31),
      ],
    );
  }

  Widget itemPage(int index) {
    for (int i = 0; i < 100; i++) {
      list.add("list $index -- $i");
    }
    setState(() {});
    return ListView.builder(
      itemBuilder: (BuildContext context, num index) {
        return Container(
            width: 80,
            height: 80,
            child: Text(list[index]),
            margin: EdgeInsets.all(10),
            color: Colors.grey);
      },
      itemCount: list.length,
    );
  }
}
